<template>
  <div class="container h100 flex-column flex-justify-center">
    <div v-if="resultType === 'login'" class="home">
      <div class="loading animate">
        <div class="square" />
        <div class="square" />
        <div class="square" />
        <div class="square" />
      </div>
      <div class="text">授权中…</div>
    </div>

    <el-result
      v-else-if="resultType === 'success'"
      icon="success"
      title="授权成功"
      sub-title="请前往默认浏览器查看~"
    />

    <el-result
      v-else-if="resultType === 'error'"
      icon="error"
      title="授权失败"
      :sub-title="errorMsg"
    />
  </div>
</template>

<script setup>
import useUserStore from '@/store/modules/user'
import VueKeycloakJs from '@dsb-norge/vue-keycloak-js'
import { useRouter } from 'vue-router'
import { createApp } from 'vue'
import storage from '@/util/storage'
import { getUserInfo } from '@/api/auth'

const app = createApp()
const router = useRouter()
const userStore = useUserStore()
const resultType = ref('login') // success error
const errorMsg = ref('')

if (userStore.token) {
  // url中带有token，代表是登录过的
  storage.local.set('token', userStore.token)
  userStore.SET_TOKEN(userStore.token)
  getUserInfo().then(rst => {
    userStore.SET_USER_INFO(rst)
    router.replace('/')
  }).catch(e => {
    resultType.value = 'error'
    errorMsg.value = e.result
  })
} else  {
  app.use(VueKeycloakJs, {
    init: {
      onLoad: 'login-required',
      redirectUri: location.origin + location.pathname,
      checkLoginIframe: false
    },
    config: {
      url: import.meta.env.VITE_APP_KEYCLOAK_URL,
      clientId: import.meta.env.VITE_APP_KEYCLOAK_CLIENT_ID,
      realm: import.meta.env.VITE_APP_KEYCLOAK_REALM
    },
    onReady(keycloak) {
      app.config.globalProperties.$keyclock = keycloak
      userStore.setKeycloak(keycloak)
      storage.local.set('keycloak', keycloak)
      storage.local.set('token', keycloak.token)
      userStore.SET_TOKEN(keycloak.token)
      getUserInfo().then(res => {
        console.log(res)
        storage.local.set('userInfo', res)
        userStore.SET_USER_INFO(res)
        router.replace('/')
      }).catch(e => {
        resultType.value = 'error'
        errorMsg.value = e.result
      })
    }
  })
}

</script>

<style scoped lang="scss">
.home {
  position: absolute;
  z-index: 10000;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  user-select: none;
  color: #736477;
  background-color: snow;
}
.home .loading {
  height: 40px;
  width: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
}
.home .square {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 20px;
  width: 20px;
}
.home .square::before {
  content: "";
  width: 10px;
  height: 10px;
  border-radius: 15%;
  border: 3px solid #8c858f;
}
.home .loading.animate .square::before {
  animation: square-to-dot-animation 1.2s linear infinite;
}
.home .loading.animate .square:nth-child(1)::before {
  animation-delay: calc(100ms * 1);
}
.home .loading.animate .square:nth-child(2)::before {
  animation-delay: calc(100ms * 2);
}
.home .loading.animate .square:nth-child(3)::before {
  animation-delay: calc(100ms * 3);
}
.home .loading.animate .square:nth-child(4)::before {
  animation-delay: calc(100ms * 4);
}
@keyframes square-to-dot-animation {
  15%,
  55% {
    border-radius: 100%;
    width: 0;
    height: 0;
    margin: 5px;
    border-width: 5px;
  }
  70% {
    border-radius: 15%;
    width: 10px;
    height: 10px;
    margin: initial;
    border-width: 3px;
  }
}
.home .text {
  font-size: 24px;
  margin-top: 20px;
}
</style>
